//# sourceURL=http://uMapApp/Module/Bridge/BridgeWeatherDetectionNode
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../../UI/Layer"
    , "../../Earth/ViewPort"
], function (
    mhjs
    , uMap
    , uEarth
    , Layer
    , ViewPort
) {

    function BridgeWeatherDetectionNode(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        _this.status = _this.dic.status[_this.status || "0"];
        // _this.type = _this.dic.type[_this.typeCode || "0"];

        if (_this.position) {

            _this.title = _this["名称"];
            _this.searchKey = _this.title;
            _this.sortKey = _this.title;

            _this.layer = new Layer({
                "app": _this.app, "viewport": new ViewPort({
                    "center": _this.position, "hpr": {
                        "heading": 23, "pitch": -20, "range": 100
                    }
                })
            });

            if (_this.dataSource) {
                _this.layer.dataSource = _this.dataSource;

                if (_this.layer.dataSourceView.tileSet) {
                    _this.layer.dataSourceView.tileSet.on("click", function () {
                        _this.trigger("click");
                    });
                }
            }

            _this.marker = new uMap.Entity({
                objectOption: {
                    position : _this.position, point: new uMap.PointGraphic({
                        objectOption: {
                            color                   : [_this.status.color],
                            pixelSize               : 10,
                            disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    }), label: new uMap.LabelGraphic({
                        objectOption: {
                            text                    : _this["名称"],
                            verticalOrigin          : "TOP",
                            pixelOffset             : [0, 10],
                            disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    })
                }
            });
            _this.layer.dataSources.entities.add(_this.marker.object);

            _this.marker.on("click", function () {
                _this.trigger("click");
            });

        }


    }

    var _class = BridgeWeatherDetectionNode;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"              : {
            value: function () {

                var _this = this;

                if (!_this.isOpened) {

                    _this.layer && _this.layer.open();
                    _this.isOpened = true;
                    _this.trigger("open");
                }
            }
        },
        "close"             : {
            value: function () {

                var _this = this;

                if (_this.isOpened) {

                    _this.layer && _this.layer.close();
                    _this.isOpened = false;
                    _this.trigger("close");
                }
            }
        },
        "toggle"            : {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active"            : {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {

                        if (_this.layer) {
                            // _this.layer.active();
                        }

                        _this.marker.label.fillColor = new uMap.Color(["#ff0"]);
                        _this.marker.label.scale = 1.2;
                        _this.marker.point.pixelSize = 15;

                        _this.trigger("active");

                    } else {

                        if (_this.layer) {
                            // _this.layer.dataSourceView.close();
                        }

                        _this.marker.label.fillColor = new uMap.Color(["#FFF"]);
                        _this.marker.label.scale = 1;
                        _this.marker.point.pixelSize = 10;

                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }


            }
        },
        "formatProperties"  : {
            value: function (data) {
                var _this = this;

                var re = {
                    "timestamp": data.dataObj.timestamp,
                    "data"     : {}
                };

                if (_this.dic.data && data.dataObj.records) {

                    $.each(data.dataObj.records, function (i, o) {

                        if (_this.dic.data[o.code]) {
                            re.data[_this.dic.data[o.code].label] = {
                                "value": o.value,
                                "unit" : _this.dic.data[o.code].unit,
                                "alarm": (_this.dic.data[o.code].threshold !== undefined && Number(o.value) < Number(_this.dic.data[o.code].threshold))
                            };
                        } else {
                            //非监测数据
                            console.log(o);
                        }

                    });
                }
                return re;
            }
        },
        "formatAlarm"       : {
            value: function (alarm, type) {
                var _this = this;

                // status
                // {
                //     "bridgeMeteId": "5b5d635c2af981946f9d02691780c879",
                //     "createTime": "2025-01-05 09:06:26",
                //     "dataObj": {
                //     "records": [
                //         {
                //             "code": "11",
                //             "state": "3"
                //         }
                //     ],
                //         "timestamp": "2025-01-05T09:06:26.558624696"
                //     },
                //     "id": "746f5f01bb7077fa603c24519319ad34",
                //     "type": 1,
                //     "rowId": 1
                // }

                // "0": {
                //     "label": "干状态",
                //         "state": {
                //         "0": {
                //             "label": "正常"
                //         }
                //     }
                // }

                // threshold
                // {
                //     "records":[
                //         {
                //             "code":"65297",
                //             "state":"-1",
                //             "value":"-0.2"
                //         }
                //         ],
                //     "timestamp":"2025-01-11 06:38:56"
                // }

                var re = {
                    "timestamp"   : alarm.dataObj.timestamp,
                    "alarmType"   : type,
                    "alarmSubType": "",
                    "alarmStatus" : "",
                    "alarmValue"  : "",
                    "alarmUnit"   : ""
                };

                if (type === "status") {
                    if (_this.dic.statusAlarm && alarm.dataObj.records && alarm.dataObj.records.length > 0) {

                        var record = alarm.dataObj.records[0];

                        if (_this.dic.statusAlarm[record.code]) {

                            re.alarmSubType = _this.dic.statusAlarm[record.code].label;
                            re.alarmStatus = _this.dic.statusAlarm[record.code].state[record.state].label;
                            re.alarmValue = record.value;

                        }
                    }

                } else if (type === "threshold") {

                    if (_this.dic.data && alarm.dataObj.records && alarm.dataObj.records.length > 0) {

                        var record = alarm.dataObj.records[0];

                        if (_this.dic.data[record.code]) {

                            re.alarmSubType = _this.dic.data[record.code].label;
                            re.alarmStatus = (record.state === "-1" ? "超低限" : "正常");
                            re.alarmValue = record.value;
                            re.alarmUnit = _this.dic.data[record.code].unit;

                        }
                    }

                }

                return re;
            }
        },
        "loadProperties"    : {
            value: function (callback) {
                var _this = this;

                if (!_this.isPropertiesLoaded && _this.dataRequest) {

                    _this.dataRequest.send({
                        "bridgeMeteId": _this.id,
                        "orderByName" : "createTime",
                        "orderByType" : "DESC",
                        "type"        : 0,
                        "pageSize"    : 1,
                        "pageNo"      : 1
                    }, function (json) {
                        _this.propertiesData = _this.formatProperties(json.data.list[0]);
                        _this.isPropertiesLoaded = true;

                        callback && callback();
                    });
                } else {
                    callback && callback();
                }


            }
        },
        "loadData"          : {
            value: function (startTime, endTime, callback) {
                var _this = this;

                if (!_this.isDataLoaded && _this.dataRequest) {

                    _this.app.queryManager.modalityLoading.open();
                    _this.app.queryManager.modalityLoading.info = "正在加载节点数据";

                    // startTime=2025-01-01&endTime=2025-02-01&type=1 1表示监测数据 2 表示告警数据 3表示告警阀值

                    _this.dataRequest.send({
                        "bridgeMeteId": _this.id,
                        "orderByName" : "createTime",
                        "orderByType" : "DESC",
                        "startTime"   : startTime,
                        "endTime"     : endTime,
                        "type"        : 0
                    }, function (json) {
                        _this.data = [];

                        $.each(json.data.list, function (i, o) {
                            _this.data.push(_this.formatProperties(o));
                        });

                        // console.log(_this.data);

                        _this.isDataLoaded = true;
                        _this.app.queryManager.modalityLoading.close();

                        callback && callback();
                    });
                } else {
                    callback && callback();
                }


            }
        },
        "loadStatusAlert"   : {
            value: function (startTime, endTime, callback) {
                var _this = this;

                if (!_this.isStatusAlertsLoaded && _this.dataRequest) {

                    _this.app.queryManager.modalityLoading.open();
                    _this.app.queryManager.modalityLoading.info = "正在加载状态告警数据";

                    // startTime=2025-01-01&endTime=2025-02-01&type=1 1表示监测数据 2 表示告警数据 3表示告警阀值

                    _this.dataRequest.send({
                        "bridgeMeteId": _this.id,
                        // "orderByName" : "createTime",
                        // "orderByType" : "DESC",
                        // "startTime"   : startTime,
                        // "endTime"     : endTime,
                        "type": 1
                    }, function (json) {
                        _this.statusAlerts = [];

                        $.each(json.data.list, function (i, o) {
                            _this.statusAlerts.push(_this.formatAlarm(o, "status"));
                        });

                        // console.log(_this.statusAlerts);

                        _this.isStatusAlertsLoaded = true;
                        _this.app.queryManager.modalityLoading.close();

                        callback && callback();
                    });
                } else {
                    callback && callback();
                }


            }
        },
        "loadThresholdAlert": {
            value: function (startTime, endTime, callback) {
                var _this = this;

                if (!_this.isThresholdAlertsLoaded && _this.dataRequest) {

                    _this.app.queryManager.modalityLoading.open();
                    _this.app.queryManager.modalityLoading.info = "正在加载状态告警数据";

                    // startTime=2025-01-01&endTime=2025-02-01&type=1 1表示监测数据 2 表示告警数据 3表示告警阀值

                    _this.dataRequest.send({
                        "bridgeMeteId": _this.id,
                        // "orderByName" : "createTime",
                        // "orderByType" : "DESC",
                        // "startTime"   : startTime,
                        // "endTime"     : endTime,
                        "type": 2
                    }, function (json) {
                        _this.thresholdAlerts = [];

                        $.each(json.data.list, function (i, o) {
                            _this.thresholdAlerts.push(_this.formatAlarm(o, "threshold"));
                        });

                        // console.log(_this.thresholdAlerts);

                        _this.isThresholdAlertsLoaded = true;
                        _this.app.queryManager.modalityLoading.close();

                        callback && callback();
                    });
                } else {
                    callback && callback();
                }


            }
        }
    });

    return _class;

});